package com.st.lotte.util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.activation.DataSource;
import javax.naming.Context;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

/*
 * tmon.db.host=14.63.168.80
tmon.db.port=7100
tmon.db.servicename=lotte
tmon.db.user=lottehome
tmon.db.passwd=lottetest

개발서버 : 
//	String driver ="com.mysql.jdbc.Driver";
//	String url = "jdbc:mysql://192.168.26.230:3306/lotte_dev?characterEncoding=UTF8";
//	String user = "root";
//	String password = "3432";
	
운영서버 : 
	String driver ="com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://14.63.168.80:7100/lotte?characterEncoding=UTF8";
	String user = "lottehome";
	String password = "lottetest";
 * */

//운영서버 내부 IP : 
//	String driver ="com.mysql.jdbc.Driver";
//	String url = "jdbc:mysql://172.27.74.238:7100/lotte?characterEncoding=UTF8";
//	String user = "lottehome";
//	String password = "lottetest";
// * */

//신규서버 : LotteDeliveryAgentServer IP : 
//String driver ="com.mysql.jdbc.Driver";
//String url = "jdbc:mysql://14.63.163.78:7100/lotte?characterEncoding=UTF8";
//String user = "lottehome";
//String password = "lottetest";
//* */

public class Database {
	// database 접속정보 수정 ------------------------- start 
	String driver ="com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://co.sweettracker.net:3306/lotte_dev?characterEncoding=UTF8";
	String user = "root";
	String password = "3432";
	// database 접속정보 수정 ------------------------- end
	Connection conn= null;
	PreparedStatement pstmt = null;
	
	// JDBC Driver loading
	public Database(){
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
	}
	
	// 커넥션 가져오기 
	public Connection getConnection(){
		try {
			MysqlDataSource ds = new MysqlDataSource();
			ds.setUrl(url);
			ds.setUser(user);
			ds.setPassword(password);
			ds.setAllowMultiQueries(true);
			ds.setMaxAllowedPacket(1000);
			conn = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	// 커넥션 종료 
	public void close(Connection conn, java.sql.PreparedStatement pstmt, ResultSet rs){
		if (rs != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				System.out.println("database resultset error ~!!!");
			}
		}		
		
		if (pstmt != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				System.out.println("database PrepareStatement error ~!!!");
			}
		}		
		
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				System.out.println("database PrepareStatement error ~!!!");
			}
		}
	}
}
